package com.sgb.goods.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.sgb.goods.api.GoodsCallBack;
import com.sgb.goods.api.GoodsNet;
import com.sgb.goods.entity.ProListEntity;
import com.sgb.goods.entity.StorageManageCallBackEntity;
import com.sgb.goods.entity.StorageManageEntity;
import com.sgb.goods.utils.Constants;
import com.sgb.goods.utils.base.viewmodel.BaseViewModel;
import com.sgb.goods.view.adapter.StorageManageAdapter;
import com.swgk.core.base.model.entity.BaseEntity;

import java.util.List;

/**
 * 描述：商品-仓储管理
 *
 * @author qiangHeNoo
 * <p>
 * 时间：2021.11.27
 */
public class StorageManageViewModel extends BaseViewModel {
    private int page = 1;
    public int total;
    public StorageManageAdapter adapter;
    public MutableLiveData<List<StorageManageEntity>> listMutableLiveData = new MutableLiveData<>();
    public MutableLiveData<StorageManageCallBackEntity> deleteCallBackEntity = new MutableLiveData<>();
    public MutableLiveData<StorageManageCallBackEntity> editCallBackEntity = new MutableLiveData<>();
    public MutableLiveData<Boolean> showNoDataView = new MutableLiveData<>();

    public StorageManageViewModel() {
        adapter = new StorageManageAdapter();
    }

    /**
     * 查询仓储列表
     *
     * @param isLoadMore 是否加载更多
     */
    public void getStorageList(boolean isLoadMore) {
        showLoading();
        page = isLoadMore ? page + 1 : Constants.PAGE_NUM;
        GoodsNet.getInstance().getGoodsApi().getStorageList(page, Constants.PAGE_SIZE_20, "")
                .enqueue(new GoodsCallBack<BaseEntity<ProListEntity<List<StorageManageEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ProListEntity<List<StorageManageEntity>>> response) {
                total = response.getData().getTotalRow();
                if (response.getData() == null) {
                    listMutableLiveData.postValue(null);
                } else {
                    if (isLoadMore) {
                        adapter.addData(response.getData().getList());
                    } else {
                        adapter.setNewData(response.getData().getList());
                    }
                    listMutableLiveData.postValue(response.getData().getList());
                }
            }

            @Override
            public void onFail() {
                listMutableLiveData.postValue(null);
            }
        });
    }

    public void delete(String id, int position) {
        showLoading();
        GoodsNet.getInstance().getGoodsApi().deleteWareHousing(id).enqueue(new GoodsCallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> response) {
                adapter.getData().remove(position);
                adapter.notifyDataSetChanged();
                if (adapter.getData().size() == 0) {
                    showNoDataView.postValue(true);
                }
            }
        });
    }

}
